package com.khadi.testcase.view.workflow;

import info.clearthought.layout.TableLayout;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EtchedBorder;

import com.khadi.testcase.util.UIHelper;
import com.khadi.testcase.view.ITestObserver;
import com.khadi.testcase.view.IWindowContent;
import com.khadi.testcase.view.editor.SortLabelEditor;
import com.khadi.testcase.view.editor.TextEditor;
import com.khadi.testcase.view.model.HtmlFieldModel;
import com.khadi.testcase.view.model.WorkFlowDtlTableModel;
import com.khadi.testcase.view.model.WorkFlowModel;
import com.khadi.testcase.view.model.WorkflowDtlModel;

public class DefineWorkFlowTab implements IWindowContent {
	private JLabel lbPageTitleTxt;
	private JTextField tfWorkFlow;
	private JTable tblWorkFlow;
	private WorkFlowDtlTableModel workFlowDtlTableModel;
	private ITestObserver observer;
	private WorkFlowModel workFlowModel;

	public DefineWorkFlowTab(ITestObserver observer) {
		this.observer = observer;
	}

	@Override
	public Object getData() {

		return workFlowModel;
	}

	@Override
	public Component getView() {

		double[] columns = new double[] { -2, -1 };
		double[] rows = new double[] { -2, -2, -1, -2 };

		TableLayout workFlowLayout = new TableLayout(columns, rows);
		workFlowLayout.setHGap(5);
		workFlowLayout.setVGap(5);

		JPanel workFlowPanel = new JPanel(workFlowLayout);

		workFlowPanel.setBorder(BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED));

		JLabel lbPageTitle = new JLabel("Page Title");
		lbPageTitleTxt = new JLabel();

		// First Row
		workFlowPanel.add(lbPageTitle, "0,0");
		workFlowPanel.add(lbPageTitleTxt, "1,0");

		// Second Row
		JLabel lbWorkFlow = new JLabel("Work Flow Name");
		tfWorkFlow = new JTextField();

		workFlowPanel.add(lbWorkFlow, "0,1");
		workFlowPanel.add(tfWorkFlow, "1,1");

		workFlowDtlTableModel = new WorkFlowDtlTableModel(getColumnNames());
		tblWorkFlow = new JTable(workFlowDtlTableModel);
		tblWorkFlow.setRowHeight(20);

		tblWorkFlow.getColumnModel().getColumn(0).setCellEditor(
				new TextEditor());
		tblWorkFlow.getColumnModel().getColumn(4).setCellEditor(
				new SortLabelEditor());

		JComboBox comboBox = new JComboBox();
		comboBox.addItem("Click");
		comboBox.addItem("Send Keys");
		tblWorkFlow.getColumnModel().getColumn(1).setCellEditor(
				new DefaultCellEditor(comboBox));

		tblWorkFlow.getSelectionModel().setSelectionMode(
				ListSelectionModel.SINGLE_SELECTION);

		JScrollPane scrollPane = new JScrollPane(tblWorkFlow);

		workFlowPanel.add(scrollPane, "0,2,1,2");

		JPanel nextPanel = new JPanel();
		nextPanel.setLayout(new BorderLayout(10, 10));
		JLabel lbNext = new JLabel(UIHelper
				.getImageIcon(UIHelper.IMG_RIGHT_ARROW_MARK));

		lbNext.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				workFlowModel = new WorkFlowModel();
				workFlowModel.setPageTitle(UIHelper.getWebDriver().getTitle());
				workFlowModel.setPageUrl(UIHelper.getWebDriver().getCurrentUrl());
				workFlowModel.setWorkflowName(tfWorkFlow.getText());

				workFlowModel.setWorkflowDtlList(workFlowDtlTableModel
						.getData());

				observer.notify(DefineWorkFlowTab.this);
			}

		});

		nextPanel.add(lbNext, BorderLayout.EAST);

		workFlowPanel.add(nextPanel, "0,3,1,3");

		return workFlowPanel;
	}

	private String[] getColumnNames() {
		return new String[] { "Description", "Command Action", "Field Name",
				"Field type", "Execution Order" };
	}

	@Override
	public void postData(Object object) {
		List<HtmlFieldModel> list = (List<HtmlFieldModel>) object;
		lbPageTitleTxt.setText(UIHelper.getWebDriver().getTitle());

		List<WorkflowDtlModel> workflowList = new LinkedList<WorkflowDtlModel>();
		int sortOrder = 1;
		for (HtmlFieldModel htmlFieldModel : list) {
			WorkflowDtlModel model = new WorkflowDtlModel();
			model.setHtmlFieldModel(htmlFieldModel);
			model.setSortOrder(sortOrder++);
			workflowList.add(model);
		}
		workFlowDtlTableModel.setData(workflowList);
		
		
	}

	@Override
	public void preData(Object object) {
		// TODO Auto-generated method stub

	}
}
